<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Proxy</title>
    <style></style>
  </head>
  <body>
    <div id="app">hello</div>
    <script>
      //模拟Vue中的data选项
      let data = {
        msg: "hello",
        count: 0,
      };
      //模拟Vue实例
      let vm = new Proxy(data, {
        //执行代理行为的函数
        //当访问vm的成员会执行
        get(target, key) {
          console.log("get,key", key, target[key]);
          return target[key];
        },
        //当设置vm的成员会执行
        set(target, key, newValue) {
          console.log("set , key:", key, newValue);
          if (target[key] === newValue) {
            return;
          }
          target[key] = newValue;
          document.querySelector("#app").textContent = target[key];
        },
      });

      //测试
      vm.msg = "Hello World";
      console.log(vm.msg);
    </script>
  </body>
</html>
